/* send remaining resources to target villages */
javascript:
var arrRatio=[28,30,25];
var intMinMerchants=10;
var w='659|595 669|605 668|604 668|608 641|597 669|607 665|599 656|595 668|619 670|606 668|607 663|596 665|597 640|597 653|593 674|616 645|594 660|594 637|599 673|614';
function l(){

  var j=document;
  var k=document.forms[0];
  function m(){
    a=k.getElementsByTagName('input');
    for(p=0; p<a.length; p++){
      if(a[p].value.indexOf('OK')!=-1)a[p].click();
    }
  }
  function x(z,A){
    C=gid('Status');
    B=gid('StatusImg');
    if($chk(C)){
      C.nodeValue=z;
    } else{
      D=gid('menu_row2').parentNode.parentNode.parentNode.parentNode.parentNode.insertRow(0);
      D.align="right";
      E=D.insertCell(0);
      E.colSpan=2;
      F=document.createElement('table');
      F.className='box';
      D2=F.insertRow(0);
      E0=D2.insertCell(0);
      E1=D2.insertCell(1);
      E.appendChild(F);
      J=document.createElement('span');
      K=document.createTextNode(z);
      K.id='Status';
      J.appendChild(K);
      E0.appendChild(J);
      B=document.createElement('img');
      B.id='StatusImg';
      E1.appendChild(B);
    }
    B.src='/graphic/dots/'+A+'.png';
  }
  function n(r){
    return parseInt(r,10);
  }
  var s=j.location.search;
  if(!s.match(/\bscreen=market\b/i)||!s.match(/\bmode=send\b/i)){
    if(s.match(/\btry=confirm_send\b/i)){
      x('On Confirm Screen... clicking OK','red');
      m();
    }
    else{
      var b=s.match(/\bvillage=\d+/i);
      var c=s.match(/\bt=\d+/i);
      var s2='?screen=market&mode=send'+($chk(b)?'&'+b[0]:'')+($chk(c)?'&'+c[0]:'');
      x('On the wrong screen :'+s+':<br>\ngoing to '+s2,'red');
      j.location.search=s2;
    }
    return;
  }
  if(n(k.wood.value)>0){
    x('Values filled in... clicking OK','red');
    m();
    return;
  }
  var intSum=0;
  arrRatio.forEach(function(r,p,arrParam){
      intSum+=r;
      }
      );
  var e =[n(j.getElementById('wood').innerHTML),n(j.getElementById('stone').innerHTML),n(j.getElementById('iron').innerHTML)];
  var arrFactors=e.map(function(r,p,arrParam){
      return arrRatio[p]>0?(r)/arrRatio[p]:999999;
      }
      );
  var numFactor=Math.min.apply(Math,arrFactors);
  var arrSendResources=arrRatio.map(function(r,p,arrParam){
      return Math.floor(numFactor*r);
      }
      );
  var numSum=0;
  arrSendResources.forEach(function(r,p,arrParam){
      numSum+=r;
      }
      );
  var u='insufficient resources:'+e;
  var blSend=(intMinMerchants*1000)<=numSum;
  if(blSend){
    var intMaxMerchants=0;
    var f=j.getElementsByTagName('table');
    for(p=0;
        p<f.length;
        p++){
      v=f[p].rows[0].cells[0].innerHTML;
      if(v.indexOf('Merchants:')==0){
        intMaxMerchants=n(v.match(/\d+/i)[0]);
        break;
      }
    }
    u='too few Merchants : '+intMaxMerchants+' < '+intMinMerchants;
    blSend=(intMaxMerchants>=intMinMerchants);
  }
  if (!blSend){
    x(u,'red');
    var g=j.links;
    for(p=0;
        p<g.length;
        p++){
      if(g[p].accessKey=="d"){
        location.href=g[p].href;
        return;
      }
    }
  }
  var o=Math.min(intMaxMerchants,Math.floor(numSum/1000));
  arrLastResources=arrRatio.map(function(r,p,arrParam){
      return Math.floor(1000*o*r/intSum);
      }
      );
  var h=w.match(/\d+\|\d+/g);
  var i=h[$random(0,h.length-1)].match(/\d+/g);
  insertNumber(k.wood,arrLastResources[0]);
  insertNumber(k.stone,arrLastResources[1]);
  insertNumber(k.iron,arrLastResources[2]);
  insertNumber(k.x,i[0]);
  insertNumber(k.y,i[1]);
  x('Filled in some values ... click the link again...','green');
}
l();
